/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**

 @author jtanguay
 */
public class Mail {

  public static void envoyerMail(String receiver, String subject, String sendMessage) {

    String user = "tpjava3@gmail.com";
    String pwd = "abc123...";
    String from = "tpjava3@gmail.com";
    String to = receiver;
    String sujet = subject;
    String messageToSend = ""
            + "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n"
            + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n"
            + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"
            + "<head>\r\n"
            + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\r\n"
            + "<title>Votre Facture</title>\r\n"
            + "</head>\r\n"
            + "<body>\r\n"
            + "	<p>" + sendMessage + "</p>\r\n"
            + "</body>\r\n" + "</html>";

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
						// Adresse mail et password de l emetteur sur le serveur
        // smtp
        return new PasswordAuthentication(user, pwd);
      }
    });

    try {
      Message message = new MimeMessage(session);
      // adresse mail de l emetteur
      message.setFrom(new InternetAddress(from));
      // adresse destinataire
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
      message.setSubject(sujet);
      message.setContent(messageToSend, "text/html; charset=utf-8");

      Transport.send(message);

    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
}
